package com.yj.controller;

import com.yj.entity.Student;
import com.yj.service.StudentService;
import com.yj.util.PageResult;
import com.yj.util.R;
import com.yj.vo.StudentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@RestController    // 相当于：@Controller + @ResponseBody(将java对象转换为json字符串)
@RequestMapping("/student")
public class StudentController {

	@Autowired
	private StudentService studentService;
	/**
	 * 1. 查询所有学生列表
	 */
	@GetMapping("/list")
	public R<List<Student>> list(){
		List<Student> students = studentService.findAll();
		return R.ok(students);
	}
	/**
	 * 2. 分页查询学生信息
	 */
	@GetMapping("/findByPage/{page}/{pageSize}")
	public R<PageResult<Student>> findByPage(@PathVariable("page") Integer page,
	                                         @PathVariable("pageSize") Integer pageSize){
		PageResult<Student> pr = studentService.findByPage(page,pageSize);
		return R.ok(pr);
	}

	/**
	 * 3. 条件查询带分页
	 *  @RequestBody(required = false) StudentVO vo: 代表前端可传可不传查询参数对象
	 */
	@PostMapping("/search/{page}/{pageSize}")
	public R<PageResult<Student>> search(@PathVariable("page") Integer page,
	                                     @PathVariable("pageSize") Integer pageSize,
	                                     @RequestBody(required = false) StudentVO vo){
		PageResult<Student> pr = studentService.search(page,pageSize,vo);
		return R.ok(pr);
	}
}
